﻿using System.Web.Mvc;
using SportsStore.Domain.Abstract;
using SportsStore.Domain.Entities;
using System.Linq;
using System.Web;
using System;
using System.Web.Helpers;
using SportsStore.Domain.Concrete;
using System.Collections.Generic;

namespace SportsStore.WebUI.Controllers
{
    [Authorize]
    public class AdminController : Controller
    {
        private IProductRepository repository;
        public AdminController(IProductRepository repo)
        {
            repository = repo;
        }

        public ViewResult Index()
        {
            return View(repository.Products);
        }

        public ViewResult Edit(int productId)
        {
            Product product = repository.Products.FirstOrDefault(p => p.ProductID == productId);
            return View(product);
        }

        [HttpPost]
        public ActionResult Edit(Product product, HttpPostedFileBase image, FormCollection collection)
        {
            if (ModelState.IsValid)
            {
                if (image != null)
                {
                    product.ImageMimeType = image.ContentType;
                    product.ImageData = new byte[image.ContentLength];
                    image.InputStream.Read(product.ImageData, 0, image.ContentLength);
                }

                Category c = new Category();
                c.CategoryID = Convert.ToInt32( collection.GetValue("poronga").AttemptedValue);

                product.Category = c;

                // save the product
                repository.SaveProduct(product);
                // add a message to the viewbag
                TempData["message"] = string.Format("{0} has been saved", product.Name);
                // return the user to the list
                return RedirectToAction("Index");
            }
            else
            {
                // there is something wrong with the data values
                return View(product);
            }
        }

        public FileContentResult GetImage(int productId)
        {
            Product prod = repository.Products.FirstOrDefault(p => p.ProductID == productId);
            if (prod != null)
            {
                return File(prod.ImageData, prod.ImageMimeType);
            }
            else
            {
                return null;
            }
        }

        public ViewResult Create()
        {
            return View("Edit", new Product());
        }

        [HttpPost]
        public ActionResult Delete(int productId)
        {
            Product prod = repository.Products.FirstOrDefault(p => p.ProductID == productId);
            if (prod != null)
            {
                repository.DeleteProduct(prod);
                TempData["message"] = string.Format("{0} was deleted", prod.Name);
            }
            return RedirectToAction("Index");
        }

        public ActionResult GetChartProductosPorcategoria()
        {
            EEPCategoryRepository ag = new EEPCategoryRepository();

            //var l1 = ag.Categories.Select(c => c.Description).ToArray();

            //var key = new Chart(width: 600, height: 400)
            //   .AddSeries(
            //       chartType: "pie",
            //       legend: "Productos por categoria",
            //       xValue: l1, // new[] { "Jan", "Feb", "Mar", "Apr", "May" },
            //       yValues: new[] { "20", "20", "40", "10" })
            //   .Write();

            return null;
        }

    }
}